$(document).ready(function () {
    var pageNo = Number($("#pageNo").val());
    $("#load-more-action").click(function () {
        pageNo = pageNo + 1;
        var status = $(".load-more").attr("data-status");
        var key_name = $("#pageNo").attr("data-key");
        if (status === 0) {
            $("#load-more-action").text("暂无数据");
            return false
        }
        var _this = $(".review-list li:last");
        $.ajax({
            url: '/post/review/load_more/',
            data: {'pageNo': pageNo,"key_name":key_name},
            dataType: 'json',
            type: 'GET',
            success: function (resul) {
                var result = resul.result;
                if (result.code === 200) {
                    // alert('here');
                    var _html = loadList(result.pageList);
                    _this.after(_html);
                    if (result.size < 2) {
                        $("#load-more").hide();
                    }
                    $("#pageNo").attr('data-key',result.key_name);
                }
                else if (result.code === 400) {
                    _this.after("已经到底了");
                    $("#load-more").hide();
                }
                else {
                    alert('难道');
                }
            },
            fail: function () {
                alert('fail')
            },
            error: function () {
                alert('error');
            }
        })
    });
    $(".movie-search-action").click(function () {
        var movie_key = $(".s-movie-search").val();
        keySearch(movie_key);
    });
});

function keySearch(key) {
    var key_name = "key_name";
    if (key.length <= 0) {
        alert('不许为空');
        return false
    }
    $.ajax({
        url: '/post/review/search/',
        data: {key_name: key},
        dataType:'json',
        type: "GET",
        success: function (data) {
            var result = data.result;
            var _this = $('.review-list');
            _this.empty();
            if (result.code === 200) {
                // alert('here');
                var _html = loadList(result.data);
                _this.append(_html);
                if (result.size < 3) {
                    $("#load-more").hide();
                }
                $("#pageNo").val(result.key_name);
            }
            else if (result.code === 400) {
                _this.after("已经到底了");
                $("#load-more").hide();
            }
            else {
                alert('难道');
            }
        },
        fail: function () {
            alert('fail')
        }
    })
}
//有些数据还要量化
function loadList(resul) {
    var html = '';
    $.each($.parseJSON(resul), function (index, value) {
        var li = '<li>\n' +
            '                        <p class="review-user-avatar">\n' +
            '                            <a href="javascript:void(0)">\n' +
            '                                <img src="/media/' + value.fields.user.avatar + '" alt="#"/>\n' +
            '                            </a>\n' +
            '                        </p>\n' +
            '                        <p class="review-user-name">\n' +
            value.fields.user +
            '                        </p>\n' +
            '                        <p class="review-movie-name">\n' +
            '                        <span>\n' +
            '                            <strong>电影:</strong>\n' +
            value.fields.movie +
            '                        </span>\n' +
            '                        </p>\n' +
            '                        <p class="review-title">\n' +
            '                            <span>\n' +
            '                            <strong>图解</strong>\n' +
            value.fields.title +
            '                        </span>\n' +
            '                        </p>\n' +
            '                        <p class="review-meta"><span>\n' +
            // '                            <strong>' + value.reviewcomment_set.count + '</strong>人评论\n' +
            '                            <span>|</span>\n' +
            '                            <strong>' + value.fields.score + '</strong>赞\n' +
            '                        </span></p>\n' +
            '                        <p>\n' +
            // REW:pk.toString()
            '                            <a href="/post/review_info/' + value.pk.toString() + '/"' + ' type="button">进入</a>\n' +
            '                        </p>\n' +
            '                    </li>';
        html += li;
    });
    return html;
}